package com.hourses.controller;

import com.hourses.mapper.StoreMapper;
import com.hourses.page.Page;
import com.hourses.pojo.CurrentUser;
import com.hourses.pojo.OutStore;
import com.hourses.pojo.Result;
import com.hourses.pojo.Store;
import com.hourses.service.impl.OutStoreServiceImpl;
import com.hourses.utils.TokenUtils;
import com.hourses.utils.WarehouseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.PrivilegedAction;
import java.util.List;

/**
 * @Description:
 * @Author:LiTao
 * @Date:2025/5/8 13:44
 */
//商品出库模块   http://localhost:3000/api/outstore/outstore-add
@RestController
@RequestMapping("/outstore")
public class OutStoreController {
    //添加出库单   http://localhost:3000/api/outstore/outstore-add  Post
    //向out_store表中添加数据
    @Autowired
    private OutStoreServiceImpl outStoreService;
    @Autowired
    private TokenUtils tokenUtils;
    @PostMapping("/outstore-add")
    public Result addOutStore(@RequestBody OutStore outStore, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME)String token){
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        outStore.setCreateBy(currentUser.getUserId());
        outStore.setOutPrice(outStore.getSalePrice());
        Result result = outStoreService.addOutStore(outStore);
        return result;
    }

    //查询寻仓库列表   http://localhost:3000/api/outstore/store-list?_t=1746929304058  GET
    @Autowired
    private StoreMapper storeMapper;
    @GetMapping("/store-list")
    public Result selectAllstores(){
        List<Store> stores = storeMapper.selectAllStores();
        return Result.ok(stores);
    }

    //分页查询出库列表   http://localhost:3000/api/outstore/outstore-page-list   GET
            //前端请求参数：
                    //outStore对象：
                                // storeId
                                // productName    需要追加的属性
                                // startTime      需要追加的属性
                                // endTime        需要追加的属性
                                // isOut
                    //page对象：
                                // pageSize
                                // pageNum
                                // totalNum
            //后端响应字段：
                                //outsId
                                //storeName            需要追加的属性
                                //productName          需要追加的属性
                                //outNum
                                //isOut
                                //createName（userName） 需要追加的属性
                                //createTime
    @GetMapping("/outstore-page-list")
    public Result selectOutListByPage(OutStore outStore, Page page){
        Result result = outStoreService.selectOutListByPage(outStore, page);
        return result;
    }

    //最终确认出库   http://localhost:3000/api/outstore/outstore-confirm
    @RequestMapping("/outstore-confirm")
    public Result endSureOutStore(@RequestBody OutStore outStore){
        Result result = outStoreService.endSureOutStore(outStore);
        return result;
    }
}
